package com.tangding.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.pojo.request.user.ActUserParams;
import com.tangding.pojo.request.user.CreateParams;
import com.tangding.pojo.request.user.UpdateParams;
import com.tangding.pojo.response.MessageDto;
import com.tangding.service.msg.request.user.CreateUserRequest;
import com.tangding.service.msg.request.user.UpdateUserRequest;
import com.tangding.service.user.UserService;

@RestController
@RequestMapping("/user")
@RequiresAuthentication
public class UserController {
	
	@Autowired
	@Qualifier("userServiceImpl")
	UserService userService;
	
	@RequestMapping(value = {"/create"}, method = RequestMethod.POST)
	@RequiresPermissions(value = {"user_create"})
	public MessageDto create(CreateParams params) throws GException {
		CreateUserRequest request = new CreateUserRequest();
		request.setUid(params.getUid());
		request.setPwd(params.getPwd());
		request.setName(params.getName());
		request.setGender(params.getGender());
		
		if (params.getStartTime() != 0) {
			request.setStart(new Date(params.getStartTime() * 1000L));
		}
		
		if (params.getEndTime() != 0) {
			request.setEnd(new Date(params.getEndTime() * 1000L));
		}
		userService.create(request);
		return MessageDto.responseSuccess();
	}
	
	@RequestMapping(value = {"/update"}, method = RequestMethod.POST)
	@RequiresPermissions(value = {"user_update"})
	public MessageDto update(UpdateParams params) throws GException {
		UpdateUserRequest request = new UpdateUserRequest();
		request.setName(params.getName());
		request.setPassword(params.getPassword());
		request.setUid(params.getUid());
		String roleIdsStr = params.getRoleIds();
		String[] roleIds = roleIdsStr.split("[,]");
		List<Integer> roleIdList = new ArrayList<>();
		for (String roleId : roleIds) {
			try {
				roleIdList.add(Integer.parseInt(roleId));
			} catch (NumberFormatException e) {
				LogHelp.doLogException(e);
			}
		}
		
		userService.update(request);
		return MessageDto.responseSuccess();
	}
	
	@RequestMapping(value = {"/operateAct"}, method = RequestMethod.POST)
	@RequiresPermissions(value = {"user_update"})
	public MessageDto operateAct(ActUserParams params) throws GException {
		Date start = null;
		if (params.getStartTime() != 0) {
			start = new Date(params.getStartTime() * 1000L);
		}
		
		Date end = null;
		if (params.getEndTime() != 0) {
			end = new Date(params.getEndTime() * 1000L);
		}
		userService.actUser(params.isAct(), params.getUid(), start, end);
		return MessageDto.responseSuccess();
	}
	
	@RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
	@RequiresPermissions(value = {"user_delete"})
	public MessageDto delete(String uid) throws GException {
		userService.delete(uid);
		return MessageDto.responseSuccess();
	}
	
	@RequestMapping(value = {"/list"}, method = RequestMethod.GET)
	@RequiresPermissions(value = {"user_list", "user_delete", "user_update", "user_create"}, logical = Logical.OR)
	public MessageDto list() throws GException {
		return MessageDto.responseSuccess(userService.list());
	}
}
